package com.homework.ch06;

import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.Scanner;

/**
 * @author yang
 * @date 2024-03-26
 */
public class GetEmail {
    public static void main(String[] args) throws Exception{
        Class<Scanner> scannerClass = Scanner.class;
        Scanner scanner = scannerClass.getConstructor(InputStream.class).newInstance(System.in);
        Method method = scannerClass.getMethod("next");
        Object email = method.invoke(scanner);
        //创建String的class对象
        Class<String> stringClass = String.class;
        String s = stringClass.getConstructor(String.class).newInstance(email);
        //创建indexOf方法
        Method indexOf = stringClass.getMethod("indexOf", String.class);
        int index = (int)indexOf.invoke(s, "@");
        //创建substring方法
        Method substring = stringClass.getMethod("substring", int.class,int.class);
        String name = (String) substring.invoke(s,0,index);
        System.out.println("用户名:"+name);
    }
}
